Imports DAL
Imports MyClasses
Imports C1.Win.C1FlexGrid
Imports C1.C1PrintDocument
Imports System.Configuration
Imports System.Drawing.Printing
Imports System.IO

Public Class frmBankMargin
    Dim titlefont, headfont As Font
    Dim X1, X2 As Integer
    Dim Y As Integer

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        'Rahul_ITS@20071123 : Add New Code
        Dim intcount As Integer
        Dim intvar As Double
        Me.Cursor = Cursors.WaitCursor
        Try
            chkAll.Checked = False
            If System.Configuration.ConfigurationManager.AppSettings("CorctStat") = "Yes" Then
                ChkAmnt.Checked = True
            Else
                ChkAmnt.Checked = False
            End If
            txtsetMargin.Enabled = False
            Call showGrid()
            Call SetCaption()
            With fgBankMargin
                For intcount = 1 To .Rows.Count - 1
                    If CDbl(.Item(intcount, 4)) <> 0 Then
                        .Item(intcount, 3) = "0"
                        intvar = Math.Round(intvar + CDbl(.Item(intcount, 4)), 2)
                    End If
                Next
                txtNewTotal.Text = Format(intvar, "0.00")
                txtsetMargin.Text = ""
                chkAll.Checked = False
            End With
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnCancel_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    'Rahul_ITS@20071123 : Add New Code
    Private Sub showGrid()
        Dim intcount As Integer
        Dim intvar As Double
        Dim Amount As Boolean
        Dim dtbBankMrgn As DataTable
        Try
            txtsetMargin.Enabled = False
            lblActT.Text = ConfigurationManager.AppSettings("Currency")
            lblMarg.Text = ConfigurationManager.AppSettings("Currency")

            Amount = IIf(ChkAmnt.Checked = True, True, False)

            dtbBankMrgn = BBankMargin.GetCompanyDetails(Amount)
            With fgBankMargin
                If dtbBankMrgn.Rows.Count > 0 Then
                    .DataSource = dtbBankMrgn
                Else
                    .DataSource = Nothing
                End If

                For intcount = 1 To .Rows.Count - 1
                    If CDbl(.Item(intcount, 4)) <> 0 Then
                        intvar = Math.Round(intvar + CDbl(.Item(intcount, 4)), 2)
                    End If
                Next
            End With
            txtNewTotal.Text = Format(intvar, "0.00")
            Call SetCaption()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "BankMargin")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub frmBankMargin_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.MdiParent = Nothing
    End Sub

    Private Sub frmBankMargin_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Correction Statements")
        e.Cancel = True
    End Sub

    Private Sub frmBankMargin_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Correction Statements")
        End If
    End Sub

    'Rahul_ITS@20071123 : Add New Code
    Private Sub frmBankMargin_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            BCommon.SetFormStandards(Me)  'Vrushali....17/12/2007
            If System.Configuration.ConfigurationManager.AppSettings("CorctStat") = "Yes" Then
                ChkAmnt.Checked = True
            Else
                ChkAmnt.Checked = False
            End If
            Call showGrid()
            Call fillgrid()
            BCommon.SetTextReadOnly(txtOriginalTotal)
            BCommon.SetTextReadOnly(txtNewTotal)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmBankMargin_Load")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    'Rahul_ITS@20071123 : Add New Code
    Sub SetCaption()
        Dim intcount As Integer
        Dim intvar As Double
        Try
            With fgBankMargin
                .Cols.Count = 5
                .Cols(0).Width = 0
                .Cols(0).Visible = False
                .Cols(1).Caption = "Company Name"
                .Cols(1).AllowEditing = False
                .Cols(1).Width = Width / 5
                .Cols(2).Caption = "Amount"
                .Cols(2).AllowEditing = False
                .Cols(2).Width = Width / 6
                .Cols(2).Format = ("0.00")
                .Cols(2).TextAlign = TextAlignEnum.RightCenter
                .Cols(3).Caption = "Margin"
                .Cols(3).Width = Width / 8
                .Cols(3).Format = ("0.00")
                .Cols(3).TextAlign = TextAlignEnum.RightCenter
                .Cols(4).Caption = "New Amount"
                '.Cols(4).AllowEditing = False
                .Cols(4).Width = Width / 8
                .Cols(4).Format = ("0.00")
                .Cols(4).TextAlign = TextAlignEnum.RightCenter

                For intcount = 1 To .Rows.Count - 1
                    If CDbl(.Item(intcount, 2)) <> 0 Then
                        intvar = Math.Round(intvar + CDbl(.Item(intcount, 2)), 2)
                    End If
                Next
                txtOriginalTotal.Text = Format(intvar, "0.00")
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetCaption")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    'Rahul_ITS@20071123 : Add New Code
    Private Sub fgBankMargin_AfterEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles fgBankMargin.AfterEdit
        Dim intcount As Integer
        Dim intvar As Double
        Dim NewTotal As Double
        Try
            With fgBankMargin

                For intcount = 1 To .Rows.Count - 1
                    Select Case .Col
                        Case 3
                            If IsNumeric(.Item(intcount, 3).ToString) = False Or Val(.Item(intcount, 3)) < 0 Or (.Item(intcount, 3)).ToString = "/" Or (.Item(intcount, 3)).ToString = "*" Or (.Item(intcount, 3)).ToString = "-" Or (.Item(intcount, 3)).ToString = "+" Then
                                BCommon.ShowMsgError(10239)
                                If txtsetMargin.Text <> "" Then
                                    .Item(intcount, 3) = txtsetMargin.Text
                                Else
                                    .Item(intcount, 3) = 0
                                End If
                                e.Cancel = True
                            End If
                            intvar = (Val(.Item(intcount, 2)) * Val(.Item(intcount, 3))) / 100
                            .Item(intcount, 4) = Math.Round((Val(.Item(intcount, 2)) + Val(intvar)), 2)
                            NewTotal = Math.Round((NewTotal + Val(.Item(intcount, 4))), 2)
                            txtNewTotal.Text = Format(NewTotal, "0.00")
                        Case 4
                            If IsNumeric(.Item(intcount, 4).ToString) = False Or Val(.Item(intcount, 4)) < 0 Or (.Item(intcount, 4)).ToString = "/" Or (.Item(intcount, 4)).ToString = "*" Or (.Item(intcount, 4)).ToString = "-" Or (.Item(intcount, 4)).ToString = "+" Then
                                BCommon.ShowMsgError(10239)
                                If txtsetMargin.Text <> "" Then
                                    .Item(intcount, 4) = txtsetMargin.Text
                                Else
                                    .Item(intcount, 4) = 0
                                End If
                                e.Cancel = True
                            End If
                            'intvar = (Val(.Item(intcount, 2)) * Val(.Item(intcount, 3))) / 100
                            '.Item(intcount, 4) = Math.Round((Val(.Item(intcount, 2)) + Val(intvar)), 2)
                            NewTotal = Math.Round((NewTotal + Val(.Item(intcount, 4))), 2)
                            .Item(intcount, 3) = 0
                            txtNewTotal.Text = Format(NewTotal, "0.00")
                    End Select
                Next
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgBankMargin_AfterEdit")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
    
    Private Sub chkAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAll.CheckedChanged
        Try
            Me.Cursor = Cursors.WaitCursor
            If chkAll.Checked = True Then
                txtsetMargin.Text = ""
                txtsetMargin.Enabled = True
                txtsetMargin.Focus()
                Call fillgrid()
            Else
                txtsetMargin.Enabled = False
                txtsetMargin.Text = ""
                Call fillgrid()
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "chkAll_CheckedChanged")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtsetMargin_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtsetMargin.KeyDown        
        If e.Alt Then Exit Sub
        If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab Then
            Call fillgrid()
            chkAll.Focus()
        Else
            If e.KeyCode = 32 Then
                e.SuppressKeyPress = True
            ElseIf BCommon.ValidateText(txtsetMargin.Text, e.KeyCode, "NUM", False) = False Then
                e.SuppressKeyPress = True
            ElseIf e.Shift = True Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub

    Private Sub txtsetMargin_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtsetMargin.LostFocus
        Call fillgrid()
    End Sub

    Sub fillgrid()
        Dim intcount As Integer
        Dim intvar As Double
        Dim NewTotal As Double
        Try
            If chkAll.Checked = True Then
                If txtsetMargin.Text <> "" Then
                    With fgBankMargin
                        For intcount = 1 To .Rows.Count - 1
                            .Item(intcount, 3) = txtsetMargin.Text
                            intvar = (Val(.Item(intcount, 2)) * Val(.Item(intcount, 3))) / 100
                            .Item(intcount, 4) = Math.Round(Val(.Item(intcount, 2)) + Val(intvar), 2)
                            NewTotal = Math.Round(NewTotal + Val(.Item(intcount, 4)), 2)
                        Next
                        txtNewTotal.Text = Format(NewTotal, "0.00")
                    End With
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fillgrid")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnPrint_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim strFilePath As String
        'Dim objdoc As New C1PrintDocument
        Dim Discrp As String = String.Empty
        Dim FirmName As String = String.Empty
        Dim PinCode As String = String.Empty
        Dim addDesc1 As String = String.Empty
        Dim phone1 As String = String.Empty
        Dim i As Integer
        Me.Cursor = Cursors.WaitCursor
        Try
            Dim objRst As System.Windows.Forms.DialogResult
            With SavBankMarginFile
                objRst = .ShowDialog()
                strFilePath = .FileName
            End With

            If strFilePath = "" And objRst = Windows.Forms.DialogResult.OK Then
                'MessageBox.Show("Please enter the file name", "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
                'MessageBox.Show(BCommon.GetMessage(10386), ConfigurationManager.AppSettings("MsgTitle"), MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
                BCommon.ShowMsgError(10386)
                Exit Sub
            ElseIf objRst = Windows.Forms.DialogResult.Cancel Then
                Me.Cursor = Cursors.Default
                Exit Sub
            End If

            Dim dtFirm As DataTable
            dtFirm = BCommon.FirmInfo()
            If dtFirm.Rows.Count > 0 Then
                Discrp = Trim(dtFirm.Rows(0).Item("Description").ToString())
                FirmName = Trim(dtFirm.Rows(0).Item("FirmName").ToString())
                PinCode = Trim(dtFirm.Rows(0).Item("PinCode").ToString())
                addDesc1 = Trim(dtFirm.Rows(0).Item("addDesc1").ToString())
                phone1 = Trim(dtFirm.Rows(0).Item("phone1").ToString())
            End If

            Dim pdf As New C1.C1Pdf.C1PdfDocument()
            Dim rc As RectangleF = pdf.PageRectangle
            Dim TextSize As New System.Drawing.SizeF
            Dim TextSize1 As New System.Drawing.SizeF
            rc.Inflate(-72, -72)
            pdf.PaperKind = PaperKind.A4

            Dim X As Integer = pdf.PageRectangle.Left
            Dim Y As Integer = pdf.PageRectangle.Top
            Dim W As Integer = pdf.PageRectangle.Width
            Dim TW As Integer = pdf.PageRectangle.Width
            W = W - 100
            Y = Y + 100
            X = X + 50
            Dim font As Font = New Font("Arial", 9)
            Dim hfont As Font = New Font("Arial", 12, FontStyle.Bold)
            Dim shfont As Font = New Font("Arial", 9, FontStyle.Bold)
            'HEADER 
            Dim sf As New StringFormat()
            sf.Alignment = StringAlignment.Center
            sf.LineAlignment = StringAlignment.Center
            TextSize = pdf.MeasureString(FirmName, hfont)
            Dim rc1 As New RectangleF(X, Y, W, TextSize.Height)
            pdf.DrawString(FirmName, hfont, Brushes.Black, rc1, sf)
            ' center align string
            Y = Y + TextSize.Height
            TextSize = pdf.MeasureString(FirmName & "TT", hfont)
            rc1 = New RectangleF(X, Y, W, TextSize.Height)
            pdf.DrawString(addDesc1 & "," & Discrp & "-" & PinCode & ", Tel: " & phone1, font, Brushes.Black, rc1, sf)
            Y = Y + TextSize.Height
            TextSize = pdf.MeasureString("Date: " & Format(Date.Now.Date(), "dd/MM/yy"), font)
            rc1 = New RectangleF(TW - TextSize.Width - 100, Y, TextSize.Width, TextSize.Height)
            pdf.DrawString("Date:" & Format(Date.Now.Date(), "dd/MM/yy"), font, Brushes.Black, rc1)
            Y = Y + TextSize.Height
            TextSize = pdf.MeasureString("Inventory Statement", hfont)
            rc1 = New RectangleF(X, Y, W, TextSize.Height)
            Y = Y + TextSize.Height
            pdf.DrawString("Inventory Statement", hfont, Brushes.Black, rc1, sf)
            Y = Y + TextSize.Height
            'HEADING LINE 
            pdf.DrawLine(Pens.Black, X, Y, W, Y)
            Dim rc2 As New RectangleF
            Dim rc3 As New RectangleF
            TextSize = pdf.MeasureString("S.No.", hfont)
            rc1 = New RectangleF(X, Y + 1, W / 2, TextSize.Height)
            rc2 = New RectangleF(X + (W / 7), Y + 1, W / 5, TextSize.Height)
            rc3 = New RectangleF(X + (W / 1.4), Y + 1, W / 3, TextSize.Height)
            pdf.DrawString("S.No.", shfont, Brushes.Black, rc1)
            pdf.DrawString("Company Name", shfont, Brushes.Black, rc2)
            pdf.DrawString("Amount", shfont, Brushes.Black, rc3)
            Y = Y + TextSize.Height
            pdf.DrawLine(Pens.Black, X, Y, W, Y)
            Dim sf1 As New StringFormat()
            sf1.Alignment = StringAlignment.Far
            sf1.LineAlignment = StringAlignment.Far
            'ITEMS 
            Dim dbltot As Double
            With fgBankMargin
                For i = 1 To .Rows.Count - 1
                    If .Item(i, 4) > 0 Then                        
                        rc1 = New RectangleF(X, Y + 3, W, TextSize.Height)
                        rc2 = New RectangleF(X + (W / 7), Y + 3, W, TextSize.Height)
                        rc3 = New RectangleF(X + (W / 1.52), (Y + 4) - 5, W - (X + (W / 1.3)), TextSize.Height)
                        pdf.DrawString(i, font, Brushes.Black, rc1)
                        pdf.DrawString(.Item(i, 1), font, Brushes.Black, rc2)
                        pdf.DrawString(Format(.Item(i, 4), "##,##0.00"), font, Brushes.Black, rc3, sf1)
                        Y = Y + TextSize.Height
                        dbltot += .Item(i, 4)
                    End If
                Next
                pdf.DrawLine(Pens.Black, X, Y + 3, W, Y + 3)
                Dim strRs As String = ConfigurationManager.AppSettings("Currency")
                TextSize = pdf.MeasureString(Format(dbltot, "##,##0.00"), font)
                TextSize1 = pdf.MeasureString(strRs, font)
                rc2 = New RectangleF(X + (W / 2), Y + 3, W / 5, TextSize.Height)
                pdf.DrawString("Total :", font, Brushes.Black, rc2)
                rc3 = New RectangleF(X + (W / 1.7), Y + 3, W / 5, TextSize.Height)
                pdf.DrawString(strRs & " " & Format(dbltot, "##,##0.00"), font, Brushes.Black, rc3, sf1)
            End With            
            pdf.Save(strFilePath)
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnPrint_Click_1")
            Me.Cursor = Cursors.Default
        Finally
            'objdoc.Dispose()
            'objdoc = Nothing
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Cursor = Cursors.WaitCursor
        PrintBIll()
        Me.Cursor = Cursors.Default
    End Sub

    Public Sub PrintBIll()
        Dim PSize As Integer = 30
        Dim PHi As Double
        Try
            With PrintDocument1.DefaultPageSettings
                Dim Ps As PaperSize
                PHi = PSize * 20 + 350
                Ps = New PaperSize("Cust", 800, PHi)
                .Margins.Top = 15
                .Margins.Bottom = 20
                .PaperSize = Ps
            End With

            headfont = New Font("Courier New", 16, FontStyle.Bold)
            titlefont = New Font("Courier New", 12, FontStyle.Bold)
            X1 = PrintDocument1.DefaultPageSettings.Margins.Left
            'Dim pageWidth As Integer
            'With PrintDocument1.DefaultPageSettings
            '    pageWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
            'End With
            X2 = X1 + 120
            'X3 = X2 + pageWidth * 0.5
            'W1 = X2 - X1
            'W2 = X3 - X2
            'W3 = pageWidth - X3
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
            'itm = 0
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "PrintBIll")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Try
            Y = PrintDocument1.DefaultPageSettings.Margins.Top + 10
            e.Graphics.DrawString("INVOICE", headfont, Brushes.Black, X1 + 250, Y)

            e.Graphics.DrawString("Cash/Credit Bill ", titlefont, Brushes.Black, X1, Y + 50)

            Y = PrintDocument1.DefaultPageSettings.Margins.Top + 120

            With PrintDocument1.DefaultPageSettings

                e.Graphics.DrawLine(Pens.Black, .Margins.Left, Y + 20, .PaperSize.Width - .Margins.Right, Y + 20)

            End With

            e.Graphics.DrawString("SNO", titlefont, Brushes.Black, X1, Y)
            e.Graphics.DrawString("NAME", titlefont, Brushes.Black, X2 - 50, Y)
            e.Graphics.DrawString("QTY", titlefont, Brushes.Black, X2 + 220, Y)
            e.Graphics.DrawString("RATE", titlefont, Brushes.Black, X2 + 280, Y)
            e.Graphics.DrawString("MRP", titlefont, Brushes.Black, X2 + 345, Y)
            e.Graphics.DrawString("TOTAL", titlefont, Brushes.Black, X2 + 425, Y)
            Y = Y + 30
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "PrintDocument1_PrintPage")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub fgBankMargin_BeforeEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles fgBankMargin.BeforeEdit
        Try
            With fgBankMargin
                Select Case .Col
                    Case 4
                        If Not (IsNothing(.Item(.Row, 2)) OrElse Trim(.Item(.Row, 2)) = "" OrElse .Item(.Row, 2) <= 0) Then
                            e.Cancel = True
                        End If
                End Select
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "fgBankMargin_BeforeEdit")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub fgBankMargin_SetupEditor(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles fgBankMargin.SetupEditor
        If e.Col = 3 Then
            Dim ctl As Control = fgBankMargin.Editor
            CType(ctl, TextBox).MaxLength = 7
        End If
    End Sub

    Private Sub ChkAmnt_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ChkAmnt.CheckedChanged
        Me.Cursor = Cursors.WaitCursor
        Call showGrid()
        Me.Cursor = Cursors.Default
    End Sub
End Class